"""
Copyright (c) 2023 - present SKbioinfo
"""
from flask_restful import Resource
from flask import request,jsonify
from flask_jwt_extended import jwt_required
from apps import db
from apps.API.pipeExecutor.models import SampleSheet

'''URL:/api/customData/UpdateDataForm/<string:tag>'''
class interfaceUpdateDataForm(Resource):
    @jwt_required()
    def post(self,tag):
        try:
            if tag == "rNGS":
                insert_data=[]
                duplicate_data=[]
                for item in request.json["data"]:
                    tmp_dict={
                        "group":0,
                        "host":"human",
                        "raw_1":item["raw_1"],
                        "raw_2":item["raw_2"],
                        "sample_name":item["sample_name"],
                        "split_id":item["split_id"]
                    }
                    count=SampleSheet.query.filter(SampleSheet.sample_name==item["sample_name"]).count()
                    if count == 0:
                        insert_data.append(tmp_dict)
                    elif count > 0:
                      duplicate_data.append(tmp_dict)  
                db.session.bulk_insert_mappings(SampleSheet, insert_data)
                db.session.commit()
                response=jsonify(duplicate_data)
                response.status=202
                return response
        except Exception as e:
            response=jsonify([])
            response.status=404
            return response
            
            